/***************************************************
//Filename:     timeofday.h                        //
//Version:	    V1.0							  //
//Published:	2005-12-04 15:30			      //
//Author:		Hua Weidong						  //


//         READ ME
**2005-12-04 15:30
1. Version V1.0 is published


****************************************************/


#ifndef	___TIMEOFDAY_H___
#define	___TIMEOFDAY_H___

#include	<stdint.h>
#include	<stdbool.h>

typedef struct __attribute__((packed))  _TIME_OF_DAY	{
	uint32_t	ms : 28;		//Component ms is the time in milliseconds after midnight.
	uint32_t	reserved : 4;
	uint16_t	days;	   		//Component days is the number of days since January 1, 1984.
	} TIME_OF_DAY;

extern bool IsLeapYear(uint16_t year);
extern bool	tod_IsValidDateTime(const TIME_OF_DAY *time);
extern TIME_OF_DAY *tod_EncodeDate(TIME_OF_DAY *time,uint16_t year,uint8_t month,uint8_t day);
extern bool tod_DecodeDateFully(const TIME_OF_DAY *time,uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *dow);
extern TIME_OF_DAY *tod_EncodeTime(TIME_OF_DAY *time,uint8_t hour,uint8_t minute,uint8_t second,uint16_t milliSecond);
extern bool tod_DecodeTimeFully(const TIME_OF_DAY *time,uint8_t *hour,uint8_t *minute,uint8_t *second,uint16_t *milliSecond);
extern bool tod_ReplaceHour(TIME_OF_DAY *time,uint8_t hour);
extern bool tod_ReplaceMinute(TIME_OF_DAY *time,uint8_t minute);
extern bool tod_ReplaceSecond(TIME_OF_DAY *time,uint8_t second);
extern bool tod_ReplaceMillisecond(TIME_OF_DAY *time,uint16_t millisecond);

extern uint16_t	tod_YearOf(const TIME_OF_DAY *time);
extern uint8_t	tod_MonthOf(const TIME_OF_DAY *time);
extern uint8_t	tod_DayOf(const TIME_OF_DAY *time);
extern uint8_t	tod_DayOfWeek(const TIME_OF_DAY *time);
extern uint8_t	tod_DayOfTheWeek(const TIME_OF_DAY *time);
extern uint8_t	tod_HourOf(const TIME_OF_DAY *time);
extern uint8_t	tod_MinuteOf(const TIME_OF_DAY *time);
extern uint8_t	tod_SecondOf(const TIME_OF_DAY *time);
extern uint16_t	tod_MilliSecondOf(const TIME_OF_DAY *time);


extern TIME_OF_DAY *tod_IncMilliSecond(TIME_OF_DAY *time,int32_t milliSecond);
extern TIME_OF_DAY *tod_IncSecond(TIME_OF_DAY *time,int32_t seconds);
extern TIME_OF_DAY *tod_IncMinute(TIME_OF_DAY *time,int32_t minutes);
extern TIME_OF_DAY *tod_IncHour(TIME_OF_DAY *time,int32_t hours);
extern TIME_OF_DAY *tod_IncDay(TIME_OF_DAY *time,int32_t days);
extern TIME_OF_DAY *tod_IncMonth(TIME_OF_DAY *time,int32_t months);
extern TIME_OF_DAY *tod_IncYear(TIME_OF_DAY *time,int32_t years);
extern TIME_OF_DAY *tod_MaxDateTime(TIME_OF_DAY *time);
extern TIME_OF_DAY *tod_MinDateTime(TIME_OF_DAY *time);
extern int32_t tod_SBetween(const TIME_OF_DAY *ANow, const TIME_OF_DAY *AThen);
extern uint32_t tod_MilliSecondsBetween(const TIME_OF_DAY *ANow, const TIME_OF_DAY *AThen);
extern uint32_t tod_SecondsBetween(const TIME_OF_DAY *ANow, const TIME_OF_DAY *AThen);
extern uint32_t tod_MinutesBetween(const TIME_OF_DAY *ANow, const TIME_OF_DAY *AThen);
extern uint32_t tod_HoursBetween(const TIME_OF_DAY *ANow, const TIME_OF_DAY *AThen);

extern uint16_t	tod_DayOfTheYear(const TIME_OF_DAY *time);

extern int8_t tod_CompareDateTime(const TIME_OF_DAY *ANow,const TIME_OF_DAY *AThen);

#endif
